﻿CREATE FUNCTION [util].[GetDurationFromDatePart]
(@Duration INT, @Datepart VARCHAR (12))
RETURNS VARCHAR (50)
AS
BEGIN
	DECLARE @RC varchar(50)
	
	SET @RC = 
		(CASE
			WHEN (@Datepart IN ('second','ss','s')) THEN
				CAST((@Duration/3600) AS varchar) + ':'
				+ RIGHT('0' + CAST(((@Duration % 3600)/60) AS varchar),2) + ':'
				+ RIGHT('0' + CAST((@Duration % 60) AS varchar),2)			
			WHEN (@Datepart IN ('minute','mi','n')) THEN
				CAST((@Duration/60) AS varchar) + ':'
				+ RIGHT('0' + CAST((@Duration % 60) AS varchar),2) + ':00'
			WHEN (@Datepart IN ('hour','hh')) THEN
				CAST(@Duration AS varchar) + ':00:00'
		END)
	
	RETURN(@RC)
END